//
//  WHYStatusCell.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/11/25.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit
import FFLabel

let WHYStatusCellMagrin: CGFloat = 12
let WHYStatusIconWidth: CGFloat = 35

/// WHYStatusCellDelegate
protocol WHYStatusCellDelegate: NSObjectProtocol {
    
    /// 点击了微博文本中的URL
    ///
    /// - parameter url: 被点击的URL
    func statusCellClickURL(url: NSURL)
    
}

class WHYStatusCell: UITableViewCell {
    
    /// StatusViewModel
    var viewModel: WHYStatusViewModel? {
        didSet {
            topView.viewModel = viewModel
            
            let text = viewModel?.status.text ?? ""
            contentLabel.attributedText = WHYEmoticonManager.sharedManager.emoticonText(text, font: contentLabel.font)
            
            pictureView.viewModel = viewModel
            
            pictureView.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pictureView.bounds.size)
            }
        }
    }
    
    weak var cellDelegate: WHYStatusCellDelegate?
    
/// 根据指定的视图模型计算行高
    func rowHeight(vm: WHYStatusViewModel) -> CGFloat {
        // 1. 记录视图模型 -> 会调用上面的 didSet 设置内容以及更新`约束`
        viewModel = vm
        
        // 2. 强制更新所有约束 -> 所有控件的frame都会被计算正确
        contentView.layoutIfNeeded()
        
        // 3. 返回底部视图的最大高度
        return CGRectGetMaxY(bottomView.frame)
    }
    
//    MARK: - 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
//        设置单元格选中的样式
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
//        super.init(coder: aDecoder)
    }
    
//    MARK: - 懒加载子控件
    private lazy var topView: WHYStatusCellTopView = WHYStatusCellTopView()
    
    lazy var contentLabel: FFLabel = FFLabel(title: "微博正文",
        titleColor: UIColor.darkGrayColor(),
        fontSize: 15,
        screenInset: WHYStatusCellMagrin)
    
    lazy var bottomView: WHYStatusCellBottomView = WHYStatusCellBottomView()
    
    lazy var pictureView: WHYStatusCellPictureView = WHYStatusCellPictureView()
    
}

// MARK: - 加载子控件
extension WHYStatusCell {
    
    func setupUI () {
        
        backgroundColor = UIColor.whiteColor()
        
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        topView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(contentView)
            make.height.equalTo(WHYStatusCellMagrin * 2 + WHYStatusIconWidth)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(WHYStatusCellMagrin)
            make.left.equalTo(contentView).offset(WHYStatusCellMagrin)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(WHYStatusCellMagrin)
            make.left.right.equalTo(contentView)
            make.height.equalTo(44)
        }
        
       contentLabel.labelDelegate = self
        
    }
}

// MARK: - FFLabelDelegate
extension WHYStatusCell: FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        if text.hasPrefix("http://") {
            
            guard let url = NSURL(string: text) else {
                return
            }
            
            cellDelegate?.statusCellClickURL(url)
        }
        
    }
    
}
